//
//  NoteDetailController.swift
//  FastV
//
//  Created by DC on 2024/6/25.
//

import UIKit

class NoteDetailController: UIViewController {

    @IBOutlet weak var deleteButton: UIButton!
    @IBOutlet weak var levelButton: UIButton!
    @IBOutlet weak var textView: UITextView!
    
    var note = NoteModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        deleteButton.isHidden = note.id.isBlank
        textView.isUserInteractionEnabled = note.id.isBlank
        
        if !note.id.isBlank {
            textView.text = note.content
        }else {
            textView.text = "Enter here"
            textView.delegate = self
        }
    }

    @IBAction func buttonClick(_ sender: UIButton) {
        view.endEditing(true)
        switch sender.tag {
        case 200:
            if note.id.isBlank {
                note.content = textView.text
                note.id = Date.getCurrentSecondString()
                note.date = Date().transform(.normal)
                YYGNoteManager.shared.addNote(note: note)
                MBProgressHUD.showInfo("Save Success")
                NotificationCenter.default.post(name: Notification.Name(Notice.kNoteChange), object: nil)
            }
             dismiss(animated: true, completion: nil)
        case 201:
            YYGNoteManager.shared.removeNote(note: note)
            DispatchQueue.main.asyncAfter(deadline: .now()+0.15, execute: DispatchWorkItem(block: {
                self.dismiss(animated: true)
                NotificationCenter.default.post(name: Notification.Name(Notice.kNoteChange), object: nil)
            }))
        case 202:
            let pop = LevelPopView.view()
            if !note.id.isBlank {

                switch note.level {
                case .Medium:
                    pop.currentIndex = 1
                case .High:
                    pop.currentIndex = 2
                default:
                    pop.currentIndex = 0
                }
                pop.show()
            }else {
                let pop = LevelPopView.view()
                pop.completion = {[weak self] index in
                    guard let `self` = self else { return  }
                    switch index {
                    case 1:
                        self.note.level = .Medium
                    case 2:
                        self.note.level = .High
                    default:
                        self.note.level = .Low
                    }
                }
                pop.show()
            }
            
        
      
        default:
            break
        }
    }
    


}

extension NoteDetailController: UITextViewDelegate {
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
          let t =  textView.text ?? ""
            if !t.isBlank {
                note.content = t
            }
            return false
        }
        return true
    }
    
}
